<?php

/**
 * template_generation actions.
 *
 * @package    master_directory
 * @subpackage template_generation
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class template_generationActions extends sfActions {
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	
	public function executeSearch(sfWebRequest $request) {
		
		$path_info = $request->getPathInfoArray ();
		$domain_name = $path_info ["SERVER_NAME"];
		//$domain_name = "SpainBusInfo.es";
		//$domain_name = "mexbusinfo.com.mx";
		//$domain_name = "busticketsto.com";
		//$domain_name = "fly-business.com";
		//$domain_name = "AbogadoDe.com";
		//$domain_name = "couturein.com";
		//$domain_name = "BilleteAutobusa.es";
		//$domain_name = "SellBuyBike.com";
		$domain_name = "USBusInfo.com";
		$directory = Doctrine_Core::getTable ( 'MasterDirectory' )->getByDomain ( $domain_name );
		
		$search_data = $directory->getSearchConfiguration ();
		
		$search_template = $search_data [0]->getTemplate ();
		$template2level_ids = array ();
		foreach ( $search_template->getTemplate2Level () as $template2level ) {
			array_push ( $template2level_ids, $template2level->getId () );
		}
		
				
		$template_content_url = Doctrine_Core::getTable ( 'TemplateContent' )->getTemplateContent ( $search_template->getId (), sfConfig::get ( 'app_content_type_url' ) );
		$url_content = $template_content_url->getTheContent ();
		
		//echo $url_content . '<br>';
		

		$matchPattern = "/#%#(.*?)#%#/";
		
		$variables_name = array ();
		$variables_str = array ();
		
		if (preg_match_all ( $matchPattern, $url_content, $matches )) {
			$variables_name = $matches [1];
			$variables_str = $matches [0];
		}
		
		$i = 0;
		foreach ( $variables_name as $variable_name ) {
			$temp_var = str_replace ( " ", "_", $variable_name );
			$var_slash = str_replace ( ".", "_", $temp_var );
			
			$cat_content_id = $request->getParameter ( $var_slash );
			
			$category_content = Doctrine_Core::getTable ( 'CategoryContent' )->find ( $cat_content_id );
			
			//echo $category_content . '<br>';
			

			if ($category_content != null) {
				$url_content = str_replace ( $variables_str [$i], str_replace ( " ", "-", trim ( $category_content->getName () ) ), $url_content );
				$i ++;
			}
		
		}
		
		//$current_uri = substr($request->getUri(), 0, strrpos($request->getUri(), "template_generation")) . str_replace(",", "", trim($url_content))  . '.html';
		$current_uri = substr ( $request->getUri (), 0, strrpos ( $request->getUri (), "template_generation" ) ) . DirectoryUtils::slugify ( $url_content ) . '.html';
	//	echo $current_uri; exit();
		return $this->redirect ( $current_uri );
	
	}
	
	public function executeLoadCategoryChildren(sfWebRequest $request) {
		
		$category_content_id = $request->getParameter ( "id" );
		
		$this->select_name = $request->getParameter ( "name" );
		
		$this->contents = Doctrine_Core::getTable ( 'CategoryContent' )->getContentChildren ( $category_content_id );
	
	}	

	
	public function executeIndex(sfWebRequest $request) {
		try {
			Doctrine_Core::getTable ( 'ContentType' )->retreiveActiveContentTypes ();
		} catch ( Exception $e ) {
			
			$path_info = $request->getPathInfoArray ();
			$domain_name = $path_info ["SERVER_NAME"];
			if (strpos ( $e, "socket" ) !== false) {
				$this->sendMailOnError ( $domain_name, $e );
			}
		}
		
		$path_info = $request->getPathInfoArray ();
		$domain_name = $path_info ["SERVER_NAME"];
		
		if (strtolower ( $domain_name ) == "pasajesautobuscr.com") {
			return $this->redirect ( "http://pasajesbuscr.com" );
		}
		
		//$domain_name = "boletosautobus.com.mx";
		//	$domain_name = "busticketsto.com";
		//$domain_name = "fly-business.com";
		

		//$domain_name = "couturein.com";
		

		//$domain_name = "BilleteAutobusa.es";
		

		//$domain_name = "AbogadoDe.com";
		//$domain_name = "SpainBusInfo.es";
		
		$url_complement = $path_info ["REQUEST_URI"];
		//$domain_name = "CoutureIn.com";
		$domain_name = "USBusInfo.com";
		

		//$this->getUser()->setAttribute("comments", array());
		

		$url = substr ( $url_complement, strrpos ( $url_complement, '/' ) + 1, strlen ( $url_complement ) - strrpos ( $url_complement, '/' ) - 6 );
		
		$domain_name = str_replace ( "www.", "", $domain_name );
		
		$directory = Doctrine_Core::getTable ( 'MasterDirectory' )->getByDomain ( $domain_name );
		
		if ($directory->getLanguageId () != null) {
			
			//echo $directory->getLanguage()->getCulture(); exit();
			

			$this->getUser ()->setCulture ( $directory->getLanguage ()->getCulture () );
		}
		
		//echo $this->getUser()->getCulture(); exit();
		

		$this->the_directory = $directory;
		
		$this->searchComponents = $this->getSearchComponents ( $directory );
		
		$this->show_comments = 0;
		
		$this->show_rating = 0;
		
		$this->comments = array ();
		
		$this->comment_fields = array ();
		
		$parent_directory = $directory;
		
		while ( $directory->getParentId () != null ) {
			$parent_directory = $directory->getMasterDirectory ();
		}
		
		$this->meta_google_verification = $directory->getGoogleSiteVerification();
		
		$this->analytics_script = $parent_directory->getAnalyticsScript ();
		
		$templates = Doctrine_Core::getTable ( 'Template' )->retreiveActiveTemplatesByDirectory ( $directory->getId () );
		
		$template_contents = array ();
		
		$involved_categories = array ();
		
		foreach ( $templates as $template ) {
			$template_content_item = Doctrine_Core::getTable ( 'TemplateContent' )->getTemplateContent ( $template->getId (), sfConfig::get ( 'app_content_type_url' ) );
			if ($template_content_item != null) {
				array_push ( $template_contents, $template_content_item );
			}
		
		}
		
		//possible template contentes with involved variables
		$possible_template_contents = $this->filterTemplateContents ( $template_contents, $url );
		/*
		 foreach ($possible_template_contents as $tc){
			echo $tc[0]->getTemplateId() . '<br>';
			$current_variables_names = $tc[1];
			foreach ($current_variables_names as $c1){
			echo 'var: ' . $c1 . '<br>';
			}
			echo '<br>';
			$current_variables_values = $tc[2];
			foreach ($current_variables_values as $c2){
			echo 'value: ' . $c2 . '<br>';
			}
			}
			exit();*/
		$template_content_found = false;
		
		$template_content_iterator = 0;
		//echo $template_content_found; exit();
		while ( ! $template_content_found && $template_content_iterator < count ( $possible_template_contents ) ) {
			//search the variables names in template2level and upper_level table to obtain corresponding level
			$current_template_content = $possible_template_contents [$template_content_iterator] [0];
			$current_variables_names = $possible_template_contents [$template_content_iterator] [1];
			$current_variables_values = $possible_template_contents [$template_content_iterator] [2];
			
			$category_content_found = array ();
			$url_variable_names = array ();
			for($i = 0; $i < count ( $current_variables_names ); $i ++) {
				$variable_name = $current_variables_names [$i];
				$level = null;
				
				$t2l = Doctrine_Core::getTable ( 'Template2Level' )->getTemplate2LevelByName ( $current_template_content->getTemplateId (), $variable_name );
				if ($t2l != null) {
					
					$level = $t2l->getLevel ();
				} else {
					$template = Doctrine_Core::getTable ( 'Template' )->find ( $current_template_content->getTemplateId () );
					
					$templates2level_ids = array ();
					foreach ( $template->getTemplate2Level () as $template2level ) {
						array_push ( $templates2level_ids, $template2level->getId () );
					}
					
					$upper_level = Doctrine_Core::getTable ( 'UpperLevel' )->getUpperLevelByName ( $templates2level_ids, $variable_name );
					if ($upper_level != null) {
						$level = $upper_level->getLevel ();
					} else {
						$side_level = Doctrine_Core::getTable ( 'SideLevel' )->getSideLevelByName ( $templates2level_ids, $variable_name );
						$level = $side_level->getLevel ();
					}
				
				}
				
				/*	$the_table = Doctrine_Core::getTable ( 'Level' );

				$the_table->setTreePath();

				$the_table->findTreePath($level);

				$tree_path = $the_table->getTreePath();

				$category_content = Doctrine_Core::getTable('CategoryContent');

				$category_content->setContentArr();

				$content = Doctrine_Core::getTable('CategoryContent')->getContentByTreePath($tree_path, DirectoryUtils::slugify($current_variables_values[$i]), $level->getCategoryId());
				*/
				
				echo '*******' . DirectoryUtils::slugify ( $current_variables_values [$i] ) . '*******<BR>' ;
				
				
				
				$content = Doctrine_Core::getTable ( 'CategoryContent' )->getCategoryContentByLevelName ( $level->getId (), DirectoryUtils::slugify ( $current_variables_values [$i] ) );
				
				//echo $content->getId();
				

				if ($content != null) {
					echo 'variable: ' . $variable_name . ' level: ' . $level->getName() . ' valor: ' . $current_variables_values[$i] . ' found <br>';
					array_push ( $category_content_found, $content );
				
				} else {
					echo $level->getId() . '<br>';
					echo 'variable: ' . $variable_name . ' level: ' . $level->getName() . ' valor: ' . $current_variables_values[$i] . ' NOT found <br>';
				}
			
			}
			if (count ( $category_content_found ) == count ( $current_variables_names )) { //preguntar por el length de los contenidos guardados
				$template_content_found = true;
			} else {
				$template_content_iterator ++;
			}
		}
		
		//	if (!$template_content_found && $url == ""){
		if (! $template_content_found) {
			if ($url != "") {
			exit();
				return $this->redirect ( "http://" . $domain_name );
				exit ();
			
			} else {
				$home_template = Doctrine_Core::getTable ( 'Template' )->getHomeTemplateByDirectory ( $directory->getId () );
				$current_template_content = Doctrine_Core::getTable ( 'TemplateContent' )->getTemplateContent ( $home_template->getId (), sfConfig::get ( 'app_content_type_url' ) );
				$template_content_found = true;
			}
		}
		
		if ($template_content_found) {
			//phase 2: steal underwear
			$the_template = $current_template_content->getTemplate ();
			
			//echo $the_template->getId();
			

			$template_variables_value [0] = ($url != "") ? $the_template : null;
			$template_variables_value [1] = ($url != "") ? $possible_template_contents [$template_content_iterator] [1] : array (); //variables names
			

			$template_variables_value [2] = ($url != "") ? $category_content_found : array ();
			
			$template_id = $the_template->getId ();
			
			if (strtolower ( $domain_name ) == "busticketsto.com" && strpos ( $url, "busticketsto.com-sitemap" ) === false) {
				if ($the_template->getId () == 478 || $the_template->getId () == 477) {
					$clone_last_template_url_pattern = "buses-from-#%#Bus Origin.Origin City#%#-to-#%#Bus Destination.Destination City#%#-in-#%#Bus Origin.Origin City.Origin State#%#-to-#%#Bus Destination.Destination City.Destination State#%#";
					$clone_last_template_url_pattern = $this->getCloneURL ( $clone_last_template_url_pattern, $template_variables_value, $the_template );
					return $this->redirect ( "http://usbusinfo.com/" . $clone_last_template_url_pattern, 301 );
				} else {
					return $this->redirect ( "http://usbusinfo.com/" . $url . $ext, 301 );
				
				}
			
			}
			
			if (strtolower ( $domain_name ) == "busticketsau.com" && strpos ( $url, "busticketsau.com-sitemap" ) === false) {
				if ($the_template->getId () == 472 || $the_template->getId () == 217) {
					$clone_last_template_url_pattern = "buses-from-#%#Origin.City#%#-in-#%#Origin.City.State#%#-to-#%#Destination.City#%#-in-#%#Destination.City.State#%#";
					$clone_last_template_url_pattern = $this->getCloneURL ( $clone_last_template_url_pattern, $template_variables_value, $the_template );
					//echo $clone_last_template_url_pattern; exit();
					return $this->redirect ( "http://aussiebusinfo.com/" . $clone_last_template_url_pattern, 301 );
				} else {
					return $this->redirect ( "http://aussiebusinfo.com/" . $url . $ext, 301 );
				
				}
			
			}
			
			if (strtolower ( $domain_name ) == "busvonnach.de" && strpos ( $url, "busvonnach.de-sitemap" ) === false) {
				if ($the_template->getId () == 150 || $the_template->getId () == 151) {
					$clone_last_template_url_pattern = "bus-von-#%#Bus Abfahrt.Abfahrtsort#%#-in-#%#Bus Abfahrt.Abfahrtsort.Abfahrtsland#%#-to-#%#Bus Ankunft.Ankunftsort#%#-in-#%#Bus Ankunft.Ankunftsort.Ankunftsland#%#";
					$clone_last_template_url_pattern = $this->getCloneURL ( $clone_last_template_url_pattern, $template_variables_value, $the_template );
					//echo $clone_last_template_url_pattern; exit();
					return $this->redirect ( "http://europabusinfo.de/" . $clone_last_template_url_pattern, 301 );
				} else {
					return $this->redirect ( "http://europabusinfo.de/" . $url . $ext, 301 );
				
				}
			}
			
			if (strtolower ( $domain_name ) == "passagemonibusbr.com.br" && strpos ( $url, "passagemonibusbr.com.br-sitemap" ) === false) {
				if ($the_template->getId () == 224) {
					$clone_last_template_url_pattern = "onibus-da-#%#Origem.Cidade#%#-na-#%#Origem.Cidade.Unidade Federativa#%#-para-#%#Destino.Cidade#%#-na-#%#Destino.Cidade.Unidade Federativa#%#";
					$clone_last_template_url_pattern = $this->getCloneURL ( $clone_last_template_url_pattern, $template_variables_value, $the_template );
					return $this->redirect ( "http://brasilbusinfo.com/" . $clone_last_template_url_pattern, 301 );
				} else {
					//echo str_ireplace("onibus-de-", "onibus-da-", str_ireplace("passagens", "onibus", $url)); exit();
					return $this->redirect ( "http://brasilbusinfo.com/" . str_ireplace("onibus-de-", "onibus-da-", str_ireplace("passagens", "onibus", $url))  . $ext, 301 );
				
				}
			}
			
			if (strtolower ( $domain_name ) == "monbus.fr" && strpos ( $url, "monbus.fr-sitemap" ) === false) {
				if ($the_template->getId () == 415 || $the_template->getId () == 416) {
					$clone_last_template_url_pattern = "bus-de-#%#Origine.Ville#%#-en-#%#Origine.Ville.Province#%#-a-#%#Destination.Ville#%#-en-#%#Destination.Ville.Province#%#";
					$clone_last_template_url_pattern = $this->getCloneURL ( $clone_last_template_url_pattern, $template_variables_value, $the_template );
					return $this->redirect ( "http://francebusinfo.com/" . $clone_last_template_url_pattern, 301 );
				} else {
					return $this->redirect ( "http://francebusinfo.com/" . $url . $ext, 301 );
				
				}
			}
			
			if (strtolower ( $domain_name ) == "boletosautobus.com.mx" && strpos ( $url, "boletosautobus.com.mx-sitemap" ) === false) {
				if ($the_template->getId () == 361 || $the_template->getId () == 473) {
					$clone_last_template_url_pattern = "autobuses-de-#%#Origen.Ciudad#%#-en-#%#Origen.Ciudad.Estado#%#-a-#%#Destino.Ciudad#%#-en-#%#Destino.Ciudad.Estado#%#";
					$clone_last_template_url_pattern = $this->getCloneURL ( $clone_last_template_url_pattern, $template_variables_value, $the_template );
					return $this->redirect ( "http://mexbusinfo.com.mx/" . $clone_last_template_url_pattern, 301 );
				} else {
					return $this->redirect ( "http://mexbusinfo.com.mx/" . $url . $ext, 301 );
				
				}
			}

			if (strtolower ( $domain_name ) == "busticketsto.co.uk" && strpos ( $url, "busticketsto.co.uk-sitemap" ) === false) {
				/*if ($the_template->getId () == 219) {
					$clone_last_template_url_pattern = "bus-from-#%#Origin.City#%#-in-#%#Origin.City.Estado#%#-to-#%#Destination.City#%#-in-#%#Destination.City.Estado#%#";
					$clone_last_template_url_pattern = $this->getCloneURL ( $clone_last_template_url_pattern, $template_variables_value, $the_template );
					return $this->redirect ( "http://businfouk.co.uk/" . $clone_last_template_url_pattern, 301 );
				} else {*/
					return $this->redirect ( "http://businfouk.co.uk/" . $url . $ext, 301 );
				
				//}
			}			
			$this->template_id = $template_id;
			
			$this->show_comments = $the_template->getShowComments ();
			
			$this->show_rating = $the_template->getShowRating ();
			
			if ($the_template->getShowComments () == 1) {
				$this->comment_fields = $the_template->getCommentField ();
				$selected_comments = array ();
				
				$this->comments = $the_template->getComment ();
				
				$category_content_ids = array ();
				
				//foreach ($template_variables_value[1] as $variable) {
				for($vi = 0; $vi < count ( $template_variables_value [1] ); $vi ++) {
					array_push ( $category_content_ids, $template_variables_value [2] [$vi]->getId () );
				}
				
				foreach ( $this->comments as $acomment ) {
					$comment_category_ids = array ();
					$comments2catcontent = $acomment->getComment2categoryContent ();
					foreach ( $comments2catcontent as $comment2catcontent ) {
						array_push ( $comment_category_ids, $comment2catcontent->getCategoryContentId () );
					}
					
					$common_category_content_ids = array_intersect ( $category_content_ids, $comment_category_ids );
					
					if (count ( $common_category_content_ids ) == count ( $category_content_ids ) && count ( $category_content_ids ) != 0) {
						
						array_push ( $selected_comments, $acomment );
					
					}
				}
				
				$this->category_content_ids = $category_content_ids;
				
				$this->comments = Doctrine_Core::getTable ( 'Comment' )->sortComments ( $selected_comments, $the_template->getRatingSortingId () ); //array_reverse($selected_comments);
			

			}
			
			$content_types = Doctrine_Core::getTable ( 'ContentType' )->retreiveActiveContentTypes ();
			
			$this->custom_meta_tag = "";
			
			foreach ( $content_types as $content_type ) {
				if ($content_type->getId () != sfConfig::get ( 'app_content_type_footer' )) {
					
					$template_contents = Doctrine_Core::getTable ( 'TemplateContent' )->getTemplateContent ( $template_id, $content_type->getId () );
					if ($template_contents != null) {
						$the_content = $template_contents->getTheContent ();
						
						$variables_name = array ();
						$matchPattern = "/#%#(.*?)#%#/";
						if (preg_match_all ( $matchPattern, $the_content, $matches )) {
							$variables_name = $matches [1];
						}
						
						$variables_name = array_unique ( $variables_name );
						
						$the_content = ($url != "") ? $this->getContentValue ( $variables_name, $template_variables_value, $the_template, $the_content ) : $the_content;
						
						if ($content_type->getId () == sfConfig::get ( "app_content_type_title" )) {
							$this->title_content = $the_content;
						} else if ($content_type->getId () == sfConfig::get ( "app_content_type_body" )) {
							
							//TODO: LLAMAR A LA FUNCION Y REEMPLAZAR EL CONTENT TYPE
							

							$listing_names = array ();
							
							$output_list = array ();
							
							$matchPattern = "/%#%(.*?)%#%/";
							if (preg_match_all ( $matchPattern, $the_content, $matches )) {
								$url_content_names = array ();
								$url_contents = $template_variables_value [2];
								foreach ( $url_contents as $url_content ) {
									array_push ( $url_content_names, $url_content->getName () );
								}
								
								$listing_names = $matches [1];
								
								foreach ( $listing_names as $listing_name ) {
									
									$listing = Doctrine_Core::getTable ( 'Listing' )->retreiveListByName ( $directory->getId (), $listing_name );
									$listing_fields = $listing->getListField ();
									foreach ( $listing_fields as $listing_field ) {
										$field_content_names = array ();
										$field_selection = Doctrine_Core::getTable ( 'ListingSelection' )->getByField ( $listing_field->getId () );
										
										foreach ( $field_selection as $selection ) {
											$selection_listing_contents = Doctrine_Core::getTable ( 'ListingSelection2categoryContent' )->getByListingSelection ( $selection->getId () );
											foreach ( $selection_listing_contents as $selection_listing_content ) {
												$selectionContent = Doctrine_Core::getTable ( 'CategoryContent' )->find ( $selection_listing_content->getCategoryContentId () );
												array_push ( $field_content_names, $selectionContent->getName () );
												while ( $selectionContent->getParentId () != null ) {
													$selectionContent = Doctrine_Core::getTable ( 'CategoryContent' )->find ( $selectionContent->getParentId () );
													array_push ( $field_content_names, $selectionContent->getName () );
												}
											}
										}
										//print_r($url_content_names); exit();
										if (count ( array_intersect ( $url_content_names, array_unique ( $field_content_names ) ) ) == count ( $url_content_names )) {
											
											if ( @$output_list [$listing->getName ()] == null) {
												$field_arr = array ();
												array_push ( $field_arr, $listing_field );
												$output_list [$listing->getName ()] = $field_arr;
											} else {
												array_push ( $output_list [$listing->getName ()], $listing_field );
											}
										}
									}
								}
								
								foreach ( $listing_names as $listing_name ) {
									if ($output_list [$listing_name] != null) {
										$content_str = '<b>' . $listing_name . '</b>';
										
										$content_str .= '<ul class="listing">';
										
										foreach ( $output_list [$listing_name] as $list_field ) {
											$content_str .= '<li>' . $list_field->getName () . ': ' . $list_field->getValue () . '</li>';
										}
										
										$content_str .= "</ul>";
										
										$the_content = str_replace ( "%#%" . $listing_name . "%#%", $content_str, $the_content );
									} else {
										$the_content = str_replace ( "%#%" . $listing_name . "%#%", " ", $the_content );
									}
								}
							
							}
							$this->body_content = $the_content;
						} else if ($content_type->getId () == sfConfig::get ( "app_content_type_meta_desc" )) {
							$this->meta_description = $the_content;
						
						} else if ($content_type->getId () == sfConfig::get ( "app_content_type_meta_kw" )) {
							$this->meta_kw = $the_content;
						
						} else if ($content_type->getId () == sfConfig::get ( "app_content_type_custom_meta" )) {
							$this->custom_meta_tag = $the_content;
						}
					
					}
				} else {
					
					$footer_url = array ();
					
					$footer_caption = array ();
					
					$footer_template = $the_template->getFooter ();
					
					if ($footer_template != null) {
						$template2level_ids = array ();
						foreach ( $the_template->getTemplate2Level () as $template2level ) {
							array_push ( $template2level_ids, $template2level->getId () );
						}
						
						$template_url_content = Doctrine_Core::getTable ( 'TemplateContent' )->getTemplateContent ( $footer_template->getId (), sfConfig::get ( 'app_content_type_url' ) );
						
						$url_pattern = $template_url_content->getTheContent ();
						
						$footer_alias = $footer_template->getAliasWhenFooter ();
						
						$the_template_url_variables = $template_variables_value [1];
						
						$the_template_url_variables_values = $template_variables_value [2];
						//replace fixed content
						for($i = 0; $i < count ( $the_template_url_variables ); $i ++) {
							$url_pattern = str_replace ( "#%#" . $the_template_url_variables [$i] . "#%#", str_replace ( " ", "-", trim ( $the_template_url_variables_values [$i]->getName () ) ), $url_pattern );
							
							if (preg_match_all ( $matchPattern, $url_pattern, $matches6 )) {
								$footer_variables_name6 = $matches6 [1];
								
								$involved_parent = $this->childInvolvesParent ( $footer_variables_name6, $the_template_url_variables [$i] );
								if ($involved_parent != "") {
									$url_pattern = str_replace ( "#%#" . $involved_parent . "#%#", str_replace ( " ", "-", trim ( $the_template_url_variables_values [$i]->getParent ()->getName () ) ), $url_pattern );
								
		//$footer_alias_complete = str_replace("#%#".$involved_parent."#%#", str_replace(" ", "-",  trim($child_content->getParent()->getName())) , $footer_alias_complete);
								}
							
							}
							/*$involved_parent = $this->childInvolvesParent($the_template_url_variables, $the_template_url_variables[$i]);
							 if($involved_parent != ""){
							 echo ' involved parent----<br>' ;
							 $url_pattern = str_replace("#%#".$involved_parent."#%#", str_replace(" ", "-",  trim($the_template_url_variables_values[$i]->getParent()->getName())) , $url_pattern);
							 //$footer_alias_complete = str_replace("#%#".$involved_parent."#%#", str_replace(" ", "-",  trim($child_content->getParent()->getName())) , $footer_alias_complete);
							 }*/
							
							$footer_alias = str_replace ( "#%#" . $the_template_url_variables [$i] . "#%#", str_replace ( " ", "-", trim ( $the_template_url_variables_values [$i]->getName () ) ), $footer_alias );
						
						}
						
						//$the_content = str_replace("#%#".$variable_name."#%#", $str, $the_content);
						$matchPattern = "/#%#(.*?)#%#/";
						if (preg_match_all ( $matchPattern, $url_pattern, $matches3 )) {
							$footer_variables_name = $matches3 [1];
							
							//replace brothers
							for($i = 0; $i < count ( $the_template_url_variables ); $i ++) {
								foreach ( $footer_variables_name as $footer_variable_name ) {
									if (strpos ( $footer_variable_name, $the_template_url_variables [$i] ) !== false) {
										$found_side_level = Doctrine_Core::getTable ( 'SideLevel' )->getSideLevelByName ( $template2level_ids, $footer_variable_name );
										if ($found_side_level != null) {
											$parent = $the_template_url_variables_values [$i]->getParent ();
											$brothers = $parent->getLevel ();
											$url_pattern = str_replace ( "#%#" . $footer_variable_name [$i] . "#%#", str_replace ( " ", "-", trim ( $brothers [$found_side_level->getPosition ()] ) ), $url_pattern );
											
											$footer_alias = str_replace ( "#%#" . $footer_variable_name [$i] . "#%#", str_replace ( " ", "-", trim ( $brothers [$found_side_level->getPosition ()] ) ), $footer_alias );
										}
									}
								}
							
							}
							
							//echo $url_pattern; exit();
							

							//replace children || new involved category, each repleacement spits a url
							foreach ( $footer_variables_name as $footer_variable_name ) {
								$var_url_arr = explode ( ".", $footer_variable_name );
								if (strpos ( $url_pattern, $footer_variable_name ) !== false && count ( $var_url_arr ) == 2) {
									$footer_template2level_ids = array ();
									foreach ( $footer_template->getTemplate2Level () as $template2level ) {
										array_push ( $footer_template2level_ids, $template2level->getId () );
									}
									$footer_variable_level = $this->locateLevel ( $footer_variable_name, $footer_template, $footer_template2level_ids );
									
									$parent_content = null;
									$found = false;
									$c = 0;
									while ( ! $found && $c < count ( $the_template_url_variables ) ) {
										$template_variable_level = $this->locateLevel ( $the_template_url_variables [$c], $the_template, $template2level_ids );
										if ($footer_variable_level->getParentId () == $template_variable_level->getId ()) {
											$found = true;
											$parent_content = $the_template_url_variables_values [$c];
										}
										$c ++;
									}
									
									if ($found) {
										
										$children_content = Doctrine_Core::getTable ( 'CategoryContent' )->getContentChildren ( $parent_content->getId () );
										foreach ( $children_content as $child_content ) {
											$url = str_replace ( "#%#" . $footer_variable_name . "#%#", str_replace ( " ", "-", trim ( $child_content->getName () ) ), $url_pattern );
											$footer_alias_complete = str_replace ( "#%#" . $footer_variable_name . "#%#", str_replace ( " ", "-", trim ( $child_content->getName () ) ), $footer_alias );
											$involved_parent = $this->childInvolvesParent ( $footer_variables_name, $footer_variable_name );
											if ($involved_parent != "") {
												$url = str_replace ( "#%#" . $involved_parent . "#%#", str_replace ( " ", "-", trim ( $child_content->getParent ()->getName () ) ), $url );
												$footer_alias_complete = str_replace ( "#%#" . $involved_parent . "#%#", str_replace ( " ", "-", trim ( $child_content->getParent ()->getName () ) ), $footer_alias_complete );
											}
											array_push ( $footer_url, $url );
											array_push ( $footer_caption, $footer_alias_complete );
										}
									
									} else {
										
										/*$the_table = Doctrine_Core::getTable ( 'Level' );

										$the_table->setTreePath();
											
										$the_table->findTreePath($footer_variable_level);
											
										$outside_tree_path = $the_table->getTreePath();
											
										$category_content = Doctrine_Core::getTable('CategoryContent');
											
										$category_content->setContentArr();
											
										//$contents = Doctrine_Core::getTable('CategoryContent')->getContentsByTreePath($outside_tree_path, $footer_variable_level->getCategoryId());
									 *
									 */
										$contents = Doctrine_Core::getTable ( 'CategoryContent' )->getCategoryContentByLevel ( $footer_variable_level->getId () );
										
										foreach ( $contents as $content ) {
											$url = str_replace ( "#%#" . $footer_variable_name . "#%#", str_replace ( " ", "-", trim ( $content->getName () ) ), $url_pattern );
											$footer_alias_complete = str_replace ( "#%#" . $footer_variable_name . "#%#", str_replace ( " ", "-", trim ( $content->getName () ) ), $footer_alias );
											array_push ( $footer_url, $url );
											array_push ( $footer_caption, $footer_alias_complete );
										}
									}
								}
							}
						
						} else {
							array_push ( $footer_url, $url_pattern );
							array_push ( $footer_caption, $footer_alias );
						}
					
					}
					$this->footer_url = $footer_url;
					$this->footer_caption = $footer_caption;
				}
			}
			//search content
			$this->categories = $directory->getCategory ();
			
			$this->json_arr = $the_template->getIncludeApiCode () == 1 ? $this->getGeolocation ( $the_template, $template_variables_value, $directory->getLanguage ()->getGoogleCode () ) : null;
			
			$this->weather_arr = $the_template->getIncludeWeatherApi () == 1 ? $this->getWeather ( $the_template, $template_variables_value, $directory->getLanguage ()->getGoogleCode () ) : null;
			
			$this->origin_city = "";
			$this->origin_state = "";
			$this->destination_city = "";
			$this->destination_state = "";
			
			$this->price_formula_a = "";
			$this->price_formula_b = "";
			
			$this->include_maps_api = false;
			
			$current_template_variables = $template_variables_value [1];
			
			$categories = $directory->getCategory ();
			
			if (count ( $categories ) > 1) {
				$origin_index = $this->getMainVariableIndexByCategory ( $categories[0]->getName(), $current_template_variables );
				$destination_index = $this->getMainVariableIndexByCategory ( $categories[1]->getName(), $current_template_variables );
				
				if ($the_template->getIncludeMapsApi () == 1 && count ( $template_variables_value [2] ) > 1) {
					$this->origin_city = $template_variables_value [2] [$origin_index]->getName ();
					$origin_parent = $template_variables_value [2] [$origin_index]->getParent ();
					$this->origin_state = $origin_parent->getName ();
					
					$this->destination_city = $template_variables_value [2] [$destination_index]->getName ();
					$destination_parent = $template_variables_value [2] [$destination_index]->getParent ();
					$this->destination_state = $destination_parent->getName ();
					
					$this->price_formula_a = $directory->getPriceFormulaA ();
					$this->price_formula_b = $directory->getPriceFormulaB ();
					
					$this->include_maps_api = true;
					
				}
			}
		
		}
	}
	
	private function getMainVariableIndexByCategory($category, $current_template_variables) {
		//print_r($current_template_variables); exit();
		$variable_index = - 1;
		$index = 0;
		$found = false;
		while ( $index < count ( $current_template_variables ) && ! $found ) {
			$variable = $current_template_variables [$index];
			$variable_arr = explode ( ".", $variable );
			if (count ( $variable_arr ) == 2 && strtolower ( $variable_arr [0] ) == strtolower ( $category )) {
				$found = true;
				$variable_index = $index;
			} else {
				$index ++;
			}
		}
		return $variable_index;
	}
	
	protected function sendMailOnError($domain_name, $e) {
		$message = $this->getMailer ()->compose ( "error-reporting@donotreply.com", array ("mrp2908@gmail.com", "marilyn@everymundo.com", "rene@everymundo.com" ), "Amazon error on " . $domain_name, $e );
		
		$this->getMailer ()->send ( $message );
		exit ();
	}
	
	public function getContents($template_contents, $url) {
	
	}
	
	public function filterTemplateContents($template_contents, $url) {
		
		$filtered_template_content = array ();
		
		foreach ( $template_contents as $template_content ) {
			$the_content = $template_content->getTheContent ();
			$variables_str = array ();
			
			$matchPattern = "/#%#(.*?)#%#/";
			
			$variables_name = array ();
			$variables_str = array ();
			
			if (preg_match_all ( $matchPattern, $the_content, $matches )) {
				$variables_name = $matches [1];
				$variables_str = $matches [0];
			}
			
			$the_content = "/^" . trim ( $the_content ) . "/i";
			
			foreach ( $variables_str as $variable_str ) {
				$the_content = str_replace ( $variable_str, "(\w.*)", $the_content );
			}
			
			preg_match ( $the_content, $url, $matches2 );
			
			$var_arr = array ();
			
			$var_values = array ();
			
			$var_arr [0] = $template_content;
			
			if ($matches2 != null && $matches2 [0] == $url) {
				//variables array
				

				$var_arr [1] = $variables_name; //variables names (origin.country)
				

				//variables values (united states) --recorrer desde pos 1 hasta el  length
				for($i = 1; $i < count ( $matches2 ); $i ++) {
					array_push ( $var_values, str_replace ( "-", " ", $matches2 [$i] ) );
				}
				
				$var_arr [2] = $var_values;
				
				array_push ( $filtered_template_content, $var_arr );
			}
		}
		
		return $filtered_template_content;
	
	}
	
	public function findInvolvedLevels($template_id, $variables_name) {
		$main_levels = array ();
		
		$template = Doctrine_Core::getTable ( 'Template' )->find ( $template_id );
		$templates2level_ids = array ();
		foreach ( $templates2level as $template2level ) {
			array_push ( $templates2level_ids, $template2level->getId () );
		}
		
		foreach ( $variables_name as $variable_name ) {
			$variable_arr = explode ( ".", $variable_name );
			$level = null;
			if (count ( $variable_arr ) == 2) {
				$level = Doctrine_Core::getTable ( 'Template2LevelTable' )->getTemplate2LevelByName ( $template_id, $variable_name )->getLevel ();
			} else {
				$level = Doctrine_Core::getTable ( 'UpperLevel' )->getUpperLevelByName ( $templates2level_ids, $variable_name )->getLevel ();
			
			}
			array_push ( $main_levels, $level );
		}
		
		return $main_levels;
	}
	
	public function findMainLevels($template_id, $variables_name) {
		$main_levels = array ();
		
		foreach ( $variables_name as $variable_name ) {
			$variable_arr = explode ( ".", $variable_name );
			if (count ( $variable_arr ) == 2) {
				$level = Doctrine_Core::getTable ( 'Template2LevelTable' )->getTemplate2LevelByName ( $template_id, $variable_name )->getLevel ();
				array_push ( $main_levels, $level );
			}
		}
		
		return $main_levels;
	}
	
	function getVariablesAndContent($template_content) {
		$the_content = $template_content->getTheContent ();
		
		$variables_str = array ();
		
		if (preg_match_all ( '!#%#(\w+)#%#!', $the_content, $matches )) {
			$variables_name = $matches [1];
		}
	
	}
	
	function getContentValue($variables_name, $template_variables_value, $the_template, $the_content) {
		
		foreach ( $variables_name as $variable_name ) {
			$str = "";
			//look for the varaibla in upper_level table
			$variable_name_without_desc = str_replace ( ".description", "", $variable_name );
			
			$temp_arr = explode ( ".", $variable_name_without_desc );
			$first_point = $temp_arr [0] . "." . $temp_arr [1];
			
			$variable_found = false;
			$index = 0;
			$variable_value = null;
			while ( ! $variable_found && $index < count ( $template_variables_value [1] ) ) {
				if ($template_variables_value [1] [$index] == $first_point) {
					$variable_found = true;
					$variable_value = $template_variables_value [2] [$index];
				}
				$index ++;
			}
			
			//TODO: revisar contenido del if UPPER
			if (count ( $temp_arr ) > 2) {
				$templates2level_ids = array ();
				foreach ( $the_template->getTemplate2Level () as $template2level ) {
					array_push ( $templates2level_ids, $template2level->getId () );
				}
				
				$upper_level_variable = Doctrine_Core::getTable ( 'UpperLevel' )->getUpperLevelByName ( $templates2level_ids, $variable_name_without_desc );
				//in
				if ($upper_level_variable != null) {
					$parent_count = count ( $temp_arr ) - 2;
					while ( $parent_count > 0 ) {
						$variable_value = $variable_value->getParent ();
						$parent_count --;
					}
				} else {
					$side_level_variable = Doctrine_Core::getTable ( 'SideLevel' )->getSideLevelByName ( $templates2level_ids, $variable_name_without_desc );
					$parent = $variable_value->getParent ();
					$children = $parent->getCategoryContent ();
					
					$variable_value = $children [$side_level_variable->getPosition ()];
				
				}
			}
			$str = strpos ( $variable_name, ".description" ) == true ? $variable_value->getDescription () : $variable_value->getName ();
			
			$the_content = str_replace ( "#%#" . $variable_name . "#%#", $str, $the_content );
		
		}
		return $the_content;
	}
	
	protected function locateLevel($main_variable_name, $template, $template2level_ids) {
		//look for the variable in template2level
		$level = null;
		$templates2level = Doctrine_Core::getTable ( 'Template2Level' )->getTemplate2LevelByName ( $template->getId (), $main_variable_name );
		if ($templates2level != null) {
			$level = $templates2level->getLevel ();
		} else {
			//look for the variable in upper_level
			$upper_level = Doctrine_Core::getTable ( 'UpperLevel' )->getUpperLevelByName ( $template2level_ids, $main_variable_name );
			if ($upper_level != null) {
				$level = $upper_level->getLevel ();
			} else {
				$level = Doctrine_Core::getTable ( 'SideLevel' )->getSideLevelByName ( $template2level_ids, $main_variable_name )->getLevel ();
			}
		}
		return $level;
	}
	
	private function getSearchComponents($directory) {
		// TODO: check field name
		$search_data = $directory->getSearchConfiguration ();
		
		$search_template = $search_data [0]->getTemplate ();
		$template2level_ids = array ();
		foreach ( $search_template->getTemplate2Level () as $template2level ) {
			array_push ( $template2level_ids, $template2level->getId () );
		}
		
		$template_content_url = Doctrine_Core::getTable ( 'TemplateContent' )->getTemplateContent ( $search_template->getId (), sfConfig::get ( 'app_content_type_url' ) );
		$url_content = $template_content_url->getTheContent ();
		
		$matchPattern = "/#%#(.*?)#%#/";
		
		$variables_name = array ();
		
		if (preg_match_all ( $matchPattern, $url_content, $matches )) {
			$variables_name = $matches [1];
		}
		
		$base_array = array ();
		//creating first array
		foreach ( $variables_name as $variable_name ) {
			$pieces = explode ( ".", $variable_name );
			$category = Doctrine_Core::getTable ( 'Category' )->retreiveActiveCategoryByNameDirectory ( $directory->getId (), $pieces [0] );
			if (array_key_exists ( $category->getId (), $base_array )) {
				array_push ( $base_array [$category->getId ()], $variable_name );
			} else {
				$variable_names = array ();
				array_push ( $variable_names, $variable_name );
				$base_array [$category->getId ()] = $variable_names;
			}
		}
		
		//ordering variables from parent to son
		foreach ( $base_array as $category_id => $variableNames ) {
			usort ( $variableNames, array ($this, 'parentFirst' ) );
			$base_array [$category_id] = $variableNames;
		}
		
		$final_array = array ();
		//creating second array
		foreach ( $base_array as $category_id => $variableNames ) {
			$component_element = array ();
			if ($this->isCategoryInclusive ( $category_id, $search_data )) {
				$level = $this->locateLevel ( $variableNames [0], $search_template, $template2level_ids );
				array_push ( $component_element, $level );
				array_push ( $component_element, $variableNames );
			} else {
				$level = $this->locateLevel ( $variableNames [count ( $variableNames ) - 1], $search_template, $template2level_ids );
				$main_name = array ();
				array_push ( $main_name, $variableNames [count ( $variableNames ) - 1] );
				array_push ( $component_element, $level );
				array_push ( $component_element, $main_name );
			}
			
			array_push ( $final_array, $component_element );
		}
		
		return $final_array;
	
	}
	
	private function parentFirst($variable1, $variable2) {
		$pieces1 = explode ( ".", $variable1 );
		$pieces2 = explode ( ".", $variable2 );
		return count ( $pieces2 ) - count ( $pieces1 );
	}
	
	private function isCategoryInclusive($category_id, $search_data) {
		$isCategoryInclusive = false;
		$category_found = false;
		$i = 0;
		while ( ! $category_found && $i < count ( $search_data ) ) {
			if ($search_data [$i]->getCategoryId () == $category_id) {
				$category_found = true;
				// TODO: check field name
				$isCategoryInclusive = $search_data [$i]->getInclusive ();
			}
			$i ++;
		}
		return $isCategoryInclusive;
	}
	
	private function childInvolvesParent($variables, $child) {
		$involved_parent_variable = "";
		$index = 0;
		while ( $index < count ( $variables ) && $involved_parent_variable == "" ) {
			/*if(strpos($variables[$index], $child) !== false){
			 echo $variables[$index] . " lo contiene";
			 }else{
			 echo $variables[$index] . "no lo contiene";
			 }
			 if(strlen($variables[$index]) > strlen($child)){
			 echo " es mas largo<br>";
			 }else{
			 echo " no es mas largo<br>";
			 	
			 }*/
			if (strpos ( $variables [$index], $child ) !== false && strlen ( $variables [$index] ) > strlen ( $child )) {
				/*echo $variables[$index] . " yay<br>";
				 exit();*/
				$involved_parent_variable = $variables [$index];
			}
			$index ++;
		}
		return $involved_parent_variable;
	
	}
	
	protected function getGeolocation($the_template, $template_variables_value, $language_code) {
		
		$key = "AIzaSyB5NDUuxqQcL2VYUp3G1qn_knSXAiCIXSI";
		
		$search_type = $the_template->getSearchTypes () != null && $the_template->getSearchTypes () != "" ? "&types=" . $the_template->getSearchTypes () : "";
		
		$search_str = $the_template->getSearchStr ();
		
		$language = $language_code != "" ? "&language=" . $language_code : "";
		
		$variables_name = array ();
		$matchPattern = "/#%#(.*?)#%#/";
		if (preg_match_all ( $matchPattern, $search_str, $matches )) {
			$variables_name = $matches [1];
		}
		
		$variables_name = array_unique ( $variables_name );
		
		$search_str = $this->getContentValue ( $variables_name, $template_variables_value, $the_template, $search_str );
		
		$search_str = '&query=' . str_replace ( " ", "+", trim ( $search_str ) );
		
		$places_arr = array ();
		
		/*$address = str_replace(" ", "+", $category_content->getName());
		 $parent = $category_content;
		 while ($parent->getParentId() != null){
			$parent = $category_content->getParent();
			$address .= str_replace(" ", "+", $parent->getName());
			}

			$geolocation_xml = "http://maps.googleapis.com/maps/api/geocode/xml?address=" . $address . "&sensor=false";
			*/
		/*$geolocation_xml = "http://maps.googleapis.com/maps/api/geocode/xml?address=miami&sensor=false";

		$latitud = "";
		$longitud = "";

		try{
		$browser = new sfWebBrowser();
		$browser->get($geolocation_xml);
		$xmlstr = $browser->getResponseText();

		$xml_elements = new SimpleXMLElement($xmlstr) ;
		if (strtolower($xml_elements->status) == "ok"){
		$results = $xml_elements->result;
		$latitud = $results[0]->geometry->location->lat;
		$longitud = $results[0]->geometry->location->lng;
		}

		// if (count($xml_elements->channel->item))
		}
		catch (Exception $e){
		echo $e->getMessage();
		}
		*/
		
		//place search
		//		if ($latitud != ""){
		/*
			$geolocation_xml = "http://maps.googleapis.com/maps/api/geocode/json?address=Miami+FL&sensor=false";
			$string = file_get_contents($geolocation_xml);
			$json_arr = json_decode($string, true);

			//echo '<pre>';
			//print_r($json_arr);
			//echo '</pre>';
			//exit();


			$results = $json_arr["results"];




			$location = $results[0]["geometry"]["location"];
			$lat = $location["lat"];
			$long = $location["lng"];
			*/
		//			echo '$lat= ' . $lat . ' long = ' . $long;
		

		//$search_url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" . $lat ."," . $long. "&types=point_of_interest&sensor=false&key=" . $key;
		

		$search_url = "https://maps.googleapis.com/maps/api/place/textsearch/json?sensor=false" . $search_str . $search_type . $language . "&key=" . $key;
		
		//echo $search_url; exit(); 
		//$search_url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query=Miami&sensor=false&key=" . $key;
		

		$string = file_get_contents ( $search_url );
		$json_arr = json_decode ( $string, true );
		
		$results = $json_arr ["results"];
		
		$detailed_results = array ();
		$results_with_photo = array ();
		$result_without_photo = array ();
		
		$results_limit = $the_template->getSearchResultsCount () > 0 && $the_template->getSearchResultsCount () <= count ( $results ) ? $the_template->getSearchResultsCount () : count ( $results );
		
		for($i = 0; $i < $results_limit; $i ++) {
			$result = $results [$i];
			/*		$place = array();
			 $place["id"] = $result["id"];
			 $place["name"] = $result["name"];
			 $place["address"] = $result["formatted_address"];
			 */
			
			$details_url = "https://maps.googleapis.com/maps/api/place/details/json?reference=" . $result ["reference"] . "&sensor=false&key=" . $key;
			//echo $details_url . '<br><br><br>';
			$details_string = file_get_contents ( $details_url );
			
			$json_details_arr = json_decode ( $details_string, true );
			
			/*echo count($json_details_arr). '<br>';
			 echo '<pre>';
			 print_r($json_details_arr);
			 echo '</pre><br><br>';
			 exit();*/
			$json_detail_result = $json_details_arr ["result"];
			array_key_exists ( "photos", $json_detail_result ) ? array_push ( $results_with_photo, $json_details_arr ) : array_push ( $result_without_photo, $json_details_arr );
		
		//echo array_key_exists("photos", $x) ? 'exists' : 'doesnt exist<br>';
		

		/*foreach ($json_details_arr["result"] as $json){
				echo $json["photos"] . '<br>';
				//array_key_exists("photos", $json) ? array_push($results_with_photo, $json) : array_push($result_without_photo, $json);
				}
				*/
		
		//$detailed_results = $json_details_arr;
		

		/*foreach ($details_results as $details_result){
			 echo '<pre>';
			 print_r($details_result);
			 echo '</pre>';
			 	
				}*/
		
		}
		//exit();
		$detailed_results = array_merge ( $results_with_photo, $result_without_photo );
		
		/*			echo '<pre>';
			print_r($detailed_results);
			echo '</pre>';
			exit();*/
		
		/*
			$jsonIterator = new RecursiveIteratorIterator(
			new RecursiveArrayIterator(json_decode($search_url, TRUE)),
			RecursiveIteratorIterator::SELF_FIRST);

			foreach ($jsonIterator as $key => $val) {
			if(is_array($val)) {
			echo "$key:\n";
			} else {
			echo "$key => $val\n";
			}
			}

			*/
		
		//	}
		return $detailed_results;
	
	}
	
	protected function getWeather($the_template, $template_variables_value, $language_code) {
		$key = "t8b45czt2z8zmxmakf5z42qu";
		
		$weather_target = $the_template->getWeatherTarget ();
		
		$variables_name = array ();
		$matchPattern = "/#%#(.*?)#%#/";
		if (preg_match_all ( $matchPattern, $weather_target, $matches )) {
			$variables_name = $matches [1];
		}
		
		$variables_name = array_unique ( $variables_name );
		
		$weather_target = $this->getContentValue ( $variables_name, $template_variables_value, $the_template, $weather_target );
		
		$weather_target = str_replace ( " ", "+", trim ( $weather_target ) );
		
		$weather_target = DirectoryUtils::slugify ( $weather_target );
		
		$url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=" . $weather_target . "&format=json&num_of_days=1&key=" . $key;
		//$url = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=Sonora+Mexico&format=json&num_of_days=1&key=" . $key;
		

		$string = @file_get_contents ( $url );
		if ($string !== false) {
			$json_arr = json_decode ( $string, true );
		}
		return $json_arr;
	
	}

}
